package net.daum.codegen;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class VelocityProcess {
	private static VelocityEngine ve = new VelocityEngine(); 
	
	static {
		try {
			ve.init();			
		} catch (Exception e) {
		}
	}
		
	public static String getResult(String filename, Map<String, Object> map) throws Exception {		
		Template t = ve.getTemplate(filename, "UTF-8");
        VelocityContext context = new VelocityContext();
                
        String[] keys = map.keySet().toArray(new String[0]);
        for (int i = 0; i < keys.length; i++) {
        	context.put(keys[i], map.get(keys[i]));	
		}        
        
        context.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        context.put("codeGenUtil", CodeGenUtil.class);
        
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        
        return writer.toString(); 
	}
}